package cn.com.haopy.dataes.db.service.main.impl;

import cn.com.haopy.dataes.db.entity.main.HDNoticeAdmin;
import cn.com.haopy.dataes.db.mapper.main.HDNoticeAdminMapper;
import cn.com.haopy.dataes.db.service.main.HDNoticeAdminService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class HDNoticeAdminServiceImpl extends ServiceImpl<HDNoticeAdminMapper, HDNoticeAdmin> implements HDNoticeAdminService {

    @Override
    public Page<HDNoticeAdmin> querySelective(String title, String type, Integer adminId,
                                               Integer pageNo, Integer size, String sort, String order) {
        QueryWrapper<HDNoticeAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HDNoticeAdmin::getAdminId, adminId);

        if(StringUtils.hasText(title)){
            queryWrapper.lambda().like(HDNoticeAdmin::getNoticeTitle, "%" + title + "%");
        }

        if (type.equals("read")){
            queryWrapper.lambda().isNotNull(HDNoticeAdmin::getReadTime);
        } else if (type.equals("unread")){
            queryWrapper.lambda().isNull(HDNoticeAdmin::getReadTime);
        }

        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order),sort);
        }

        Page<HDNoticeAdmin> page = new Page<>(pageNo, size);
        return super.page(page, queryWrapper);
    }

    @Override
    public HDNoticeAdmin findByNIdAId(Integer noticeId, Integer adminId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDNoticeAdmin::getNoticeId, noticeId)
                .eq(HDNoticeAdmin::getAdminId, adminId);

        return super.getOne(queryWrapper);
    }

//    @Override
//    public boolean save(HDNoticeAdmin noticeAdmin) {
//        noticeAdmin.setCreateTime(LocalDateTime.now());
//        noticeAdmin.setUpdateTime(LocalDateTime.now());
//
//        return super.save(noticeAdmin);
//    }

//    @Override
//    public boolean updateById(HDNoticeAdmin noticeAdmin) {
//        noticeAdmin.setUpdateTime(LocalDateTime.now());
//        return super.updateById(noticeAdmin);
//    }

    @Override
    public boolean markReadByIdsAId(List<Integer> ids, Integer adminId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(HDNoticeAdmin::getId, ids)
                .eq(HDNoticeAdmin::getAdminId, adminId);

        HDNoticeAdmin noticeAdmin = new HDNoticeAdmin();
        LocalDateTime now = LocalDateTime.now();
        noticeAdmin.setReadTime(now);
        noticeAdmin.setUpdateTime(now);

        return super.update(noticeAdmin, queryWrapper);
    }

    @Override
    public boolean deleteByIdAId(Integer id, Integer adminId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDNoticeAdmin::getId, id)
                .eq(HDNoticeAdmin::getAdminId, adminId);

        HDNoticeAdmin noticeAdmin = new HDNoticeAdmin();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);

        return super.update(noticeAdmin, queryWrapper);
    }

    @Override
    public boolean deleteByIdsAId(List<Integer> ids, Integer adminId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(HDNoticeAdmin::getId, ids)
                .eq(HDNoticeAdmin::getAdminId, adminId);

        HDNoticeAdmin noticeAdmin = new HDNoticeAdmin();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);

        return super.update(noticeAdmin, queryWrapper);
    }

    @Override
    public int countUnreadByAId(Integer adminId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDNoticeAdmin::getAdminId, adminId)
                .isNull(HDNoticeAdmin::getReadTime);


        return super.count(queryWrapper);
    }

    @Override
    public List<HDNoticeAdmin> queryByNoticeId(Integer noticeId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDNoticeAdmin::getNoticeId, noticeId);

        return super.list(queryWrapper);
    }

    @Override
    public boolean deleteByNoticeId(Integer noticeId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDNoticeAdmin::getNoticeId, noticeId);

        HDNoticeAdmin noticeAdmin = new HDNoticeAdmin();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);

        return super.update(noticeAdmin, queryWrapper);
    }

    @Override
    public boolean deleteByNoticeIds(List<Integer> noticeIds) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(HDNoticeAdmin::getNoticeId, noticeIds);

        HDNoticeAdmin noticeAdmin = new HDNoticeAdmin();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);

        return super.update(noticeAdmin, queryWrapper);
    }

    @Override
    public int countReadByNoticeId(Integer noticeId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDNoticeAdmin::getNoticeId, noticeId)
                .isNotNull(HDNoticeAdmin::getReadTime);

        return super.count(queryWrapper);
    }

    @Override
    public boolean updateByNoticeId(HDNoticeAdmin noticeAdmin, Integer noticeId) {
        LambdaQueryWrapper<HDNoticeAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HDNoticeAdmin::getNoticeId, noticeId);

        noticeAdmin.setUpdateTime(LocalDateTime.now());
        return super.update(noticeAdmin, queryWrapper);
    }
}
